package com.yx.system.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.yx.common.annotation.LoginUser2;
import com.yx.common.utils.PageUtils;
import com.yx.common.utils.Query;
import com.yx.common.utils.R;
import com.yx.common.validator.Assert;
import com.yx.modules.api.entity.TbUser;
import com.yx.modules.api.service.TbUserService;
import com.yx.system.entity.SysUser;
import com.yx.system.entity.TbWithdraw;
import com.yx.system.service.TbWithdrawService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.omg.CORBA.INTERNAL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;



/**
 * 提现申请
 * 
 * @author luo
 * @email www.baidu.com
 * @date 2018-07-18 11:43:37
 */
@RestController
@RequestMapping("sys/withdraw")
public class TbWithdrawController {
	@Autowired
	private TbWithdrawService withdrawService;

	@Autowired
	private TbUserService tbUserService;

	@Autowired
	private RedisTemplate<String, String> redisTemplate;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:withdraw:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<TbWithdraw> withdrawList = withdrawService.queryList(query);
		int total = withdrawService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(withdrawList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}


	/**
	 * 作者查看自己申请列表
	 */
	@RequestMapping("/authorList")
	@RequiresPermissions("sys:withdraw:authorList")
	public R list2(@RequestParam Map<String, Object> params,@LoginUser2 SysUser sysUser){
		if (sysUser.getIdentity()==0){
			return R.error("作者才能进行操作");
		}
		//查询对应的用户
		TbUser tbUser = tbUserService.findByMobile(sysUser.getUsername());
		if (tbUser.getIdentity()==0){
			return R.error("用户的身份不是作者");
		}
		//查询列表数据
		Query query = new Query(params);
		query.put("applyid",tbUser.getUserId());

		List<TbWithdraw> withdrawList = withdrawService.queryList(query);
		int total = withdrawService.queryTotal(query);

		PageUtils pageUtil = new PageUtils(withdrawList, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sys:withdraw:info")
	public R info(@PathVariable("id") Integer id){
		TbWithdraw withdraw = withdrawService.queryObject(id);
		withdraw.setMoney(withdraw.getMoney()/100);
		withdraw.setBalance(withdraw.getBalance()/100);
		return R.ok().put("withdraw", withdraw);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sys:withdraw:save")
	public R save(@RequestBody TbWithdraw withdraw){
		withdrawService.save(withdraw);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sys:withdraw:update")
	public R update(@RequestBody TbWithdraw withdraw){
		withdrawService.update(withdraw);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sys:withdraw:delete")
	public R delete(@RequestBody Integer[] ids){
		withdrawService.deleteBatch(ids);
		
		return R.ok();
	}


	/*
	* 审核通过
	*/
	@RequestMapping("/pass")
	@RequiresPermissions("sys:withdraw:pass")
	public R pass(@RequestBody TbWithdraw withdraw){
		if (withdraw.getStatus()!=0){
			return R.error("请选择正确的订单进行审核");
		}
		//审核通过改变申请订单状态
		withdraw.setStatus(1);
		withdraw.setBalance(withdraw.getBalance()*100);
		withdraw.setMoney(withdraw.getMoney()*100);
		//找到用户，注入实时余额
		TbUser tbUser = tbUserService.foundById((long) withdraw.getApplyid());
		if (tbUser==null){
			return R.error("未查询到该用户");
		}
		withdraw.setBalance(tbUser.getBalance());
		//更新状态
		withdrawService.update(withdraw);
		return R.ok();
	}

	/*
	* 审核拒绝
	*/
	@RequestMapping("/refuse")
	@RequiresPermissions("sys:withdraw:refuse")
	public R refuse(@RequestBody TbWithdraw withdraw){
		Assert.isBlank(withdraw.getRefusereason(),"拒绝理由不能为空");
		if (withdraw.getStatus()!=0){
			return R.error("请选择正确的订单进行审核");
		}
		//审核通过改变申请订单状态
		withdraw.setStatus(2);
		withdraw.setBalance(withdraw.getBalance()*100);
		withdraw.setMoney(withdraw.getMoney()*100);
		//将用户的提现退还
		TbUser tbUser = tbUserService.foundById((long) withdraw.getApplyid());
		if (tbUser==null){
			return R.error("未查询到该用户");
		}
		tbUser.setBalance(Integer.parseInt(new java.text.DecimalFormat("0").format(tbUser.getBalance()+(withdraw.getMoney()*100))));
		tbUserService.update(tbUser);
		//更新状态
		withdraw.setBalance(tbUser.getBalance());
		withdrawService.update(withdraw);
		return R.ok();
	}

	/*
	* 打款
	*/
	@RequestMapping("/remit")
	@RequiresPermissions("sys:withdraw:remit")
	public R remit(@RequestBody TbWithdraw withdraw){
		if (withdraw.getStatus()!=1){
			return R.error("请选择正确的订单进行汇款");
		}
		//审核通过改变申请订单状态
		withdraw.setStatus(3);
		withdraw.setBalance(withdraw.getBalance()*100);
		withdraw.setMoney(withdraw.getMoney()*100);
		//找到用户，注入实时余额
		TbUser tbUser = tbUserService.foundById((long) withdraw.getApplyid());
		if (tbUser==null){
			return R.error("未查询到该用户");
		}
		withdraw.setBalance(tbUser.getBalance());
		//更新状态
		withdrawService.update(withdraw);
		return R.ok();
	}

	/*
	* 申请提现
	*/
	@RequestMapping("apply")
	@RequiresPermissions("sys:withdraw:apply")
	public R applyWithdraw(String phone,Double money,String code, @LoginUser2 SysUser sysUser){
		if (sysUser.getIdentity()==0){
			return R.error("作者才能进行操作");
		}
		//查询对应的用户
		TbUser tbUser = tbUserService.findByMobile(sysUser.getUsername());
		if (tbUser.getIdentity()==0){
			return R.error("用户的身份不是作者");
		}
		String rcode = redisTemplate.opsForValue().get(phone);
		if (rcode==null){
			return R.error("验证码已过期");
		}
		if (code.equals(rcode)) {
			//判断提现金额与余额
			Integer actual = Integer.parseInt(new java.text.DecimalFormat("0").format(money * 100 * 100));
			if (actual > tbUser.getBalance()) {
				return R.error("请输入正确的金额进行提现");
			}
			//初始化一个提现记录
			TbWithdraw tbWithdraw = new TbWithdraw();
			tbWithdraw.setBalance((tbUser.getBalance() - actual));
			tbWithdraw.setMoney(money * 100);
			tbWithdraw.setStatus(0);
			tbWithdraw.setApplyid(tbUser.getUserId());
			tbWithdraw.setApplyname(tbUser.getNickname());
			tbWithdraw.setApplyphone(phone);
			tbWithdraw.setCreatedate(new Date());
			//保存
			withdrawService.save(tbWithdraw);
			//改变用户的蝉币记录
			tbUser.setBalance(Integer.parseInt(new java.text.DecimalFormat("0").format(tbUser.getBalance()-(tbWithdraw.getMoney()*100))));
			tbUserService.update(tbUser);
			return R.ok();
		}else {
			return R.error("验证码错误");
		}
	}

	/*
	* 查看拒绝原因
	*/
	@RequestMapping("reason")
	@RequiresPermissions("sys:withdraw:reason")
	public R applyWithdraw(Integer id){
		TbWithdraw withdraw = withdrawService.queryObject(id);
		if (withdraw.getStatus()!=2){
			return R.error("请选择正确的记录查看拒绝原因");
		}
		return R.ok().put("withdraw", withdraw);
	}

}
